<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-tabs>
          <el-tab-pane label="角色管理" />
        </el-tabs>
        <!-- 添加按钮 -->
        <el-row>
          <el-button type="primary" @click="hAdd()"> + 新增角色</el-button>
        </el-row>
        <el-table
          border
          style="width: 100%"
          :data="list"
        >
          <el-table-column
            width="180"
            label="序号"
            sortable
            type="index"
          />
          <el-table-column
            width="180"
            label="角色名"
            sortable
            prop="name"
          />
          <el-table-column
            label="描述"
            sortable
            prop="description"
          />
          <el-table-column
            label="操作"
            sortable
          >
            <template v-slot="scope">
              <el-button size="small" type="success" @click="hAssign(scope.row.id)">分配权限</el-button>
              <el-button size="small" type="primary" @click="hEdit(scope.row)">编辑</el-button>
              <el-button size="small" type="danger" @click="hDel(scope.row.id)">删除</el-button>
            </template>
          </el-table-column></el-table>
        <el-pagination
          :page-size="pageList.pagesize"
          :current-page.sync="pageList.page"
          :page-sizes="[1,2, 4, 6, 8, 10]"
          :total="total"
          background
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="doSizeChange"
          @current-change="hCurrentChange"
        />
      </el-card>
      <!-- 新增弹框 -->
      <el-dialog
        :title="isEdit ? '编辑' : '新增'"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @close="resetForm"
      >
        <el-form ref="roleForm" :rules="rules" :model="roleForm" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog = false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
      <!-- 新增弹框 -->
      <el-dialog
        title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
        :visible.sync="showDialogAssign"
      >
        <assign-permission
          v-if="showDialogAssign"
          :cur-id="curId"
          @close="hClose"
          @success="hSuccess"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import assignPermission from './assignPermission'
import { getRoles, delRoles, addRole, editRole } from '@/api/settings'
export default {
  components: {
    assignPermission
  },
  data() {
    return {
      curId: '',
      showDialogAssign: false,
      // 表格数据
      list: [],
      // 总页数
      total: 0,
      // 判断是否是编辑
      isEdit: false,
      // 角色列表参数,也是页面列表的默认参数
      pageList: {
        page: 1,
        pagesize: 10
      },
      showDialog: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 请求角色列表
    this.loadRoles()
  },
  methods: {
    // 关闭
    hSuccess() {
      this.showDialogAssign = false
    },
    // 关闭弹窗
    hClose() {
      this.showDialogAssign = false
    },
    // 分配权限
    hAssign(id) {
      this.showDialogAssign = true
      // 保存id
      this.curId = id
    },
    // 请求角色列表
    async loadRoles() {
      // 发送ajax请求
      const res = await getRoles(this.pageList)
      console.log(res)
      // 渲染数据
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 添加
    hAdd() {
      // 显示弹窗
      this.showDialog = true
      // 判断是否是编辑
      this.isEdit = false
    },
    async doAdd() {
      const res = await addRole(this.roleForm)
      console.log(res)
      // 关闭弹窗
      this.showDialog = false
      // 查询最后一页数据
      this.total++
      this.pageList.page = Math.ceil(this.total / this.pageList.pagesize)
      this.$message.success('添加成功')
      this.loadRoles()
    },
    // 编辑
    async doEdit() {
      const res = await editRole(this.roleForm)
      console.log(res)
      // 关闭弹窗
      this.showDialog = false
      this.$message.success('编辑成功')
      this.loadRoles()
    },
    // 编辑模块
    hEdit(row) {
      // 数据回填
      this.roleForm = { ...row }
      // 显示弹窗
      this.showDialog = true
      // 判断是否是编辑
      this.isEdit = true
    },
    // 删除模块
    async doDel(id) {
      const res = await delRoles(id)
      console.log(res)
      this.$message.success('删除成功')
      // 如果为最后一条数据,则页码要减1
      if (this.list.length === 1) {
        this.pageList.page--
        this.pageList.page = Math.max(1, this.pageList.page)
      }
      this.loadRoles()
    },
    hDel(id) {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
        this.loadRoles()
      }).catch(() => {

      })
    },
    //  size发生变化
    doSizeChange(size) {
      console.log('页码发生变化', size)
      // 修改每页数量
      this.pageList.pagesize = size
      this.pageList.page = 1 // 每页数量变化,页码恢复到1
      // 刷新列表
      this.loadRoles()
    },
    // 页面发生变化
    hCurrentChange(page) {
      console.log('页面发生变化', page)
      // 更新页面
      this.pageList.page = page
      this.loadRoles()
    },
    hSubmit() {
      this.isEdit ? this.doEdit() : this.doAdd()
    },
    resetForm() {
      this.$refs.roleForm.resetFields()
    }
  }
}
</script>
